package com.jia.lidao.widget;

import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout;

/**
 * Created by jia on 17/11/11.
 */

public class ExpandLinearlayout extends LinearLayout {


    private boolean animRun = false;

    private Animation animation;

    private int h;

    private ViewGroup.LayoutParams params;

    public ExpandLinearlayout(Context context) {
        super(context);
    }

    public ExpandLinearlayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }




    private void setshow() {
        if(animRun)
            return;
        params = getLayoutParams();
        h=MathMeasuer(this);
        Log.e("1234","show h="+h);
        animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                params.height = (int) (interpolatedTime * h);
                Log.e("1234","params.height="+params.height);
                setLayoutParams(params);
            }
        };
        animation.setDuration(400);
        setVisibility(VISIBLE);
        startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                animRun = true;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animRun = false;
//
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


    private void setGone() {
        if(animRun)
            return;
        params = getLayoutParams();
        h=MathMeasuer(this);
        Log.e("1234","gone h="+h);
        animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                params.height = h - (int) (interpolatedTime * h);
                Log.e("1234","params.height="+params.height);
                setLayoutParams(params);
            }
        };
        animation.setDuration(400);
        startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                animRun = true;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animRun = false;
                params.height=0;
                Log.e("1234","end");
                setVisibility(GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }



    private int MathMeasuer(LinearLayout viewGroup) {
        int height = 0;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View v = viewGroup.getChildAt(i);
            int w = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
            int h = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
            v.measure(w, h);
            height += v.getMeasuredHeight();
        }
        return height;
    }

//    @Override
//    public void setVisibility(int visibility) {
//        if(visibility==VISIBLE){
//            setshow();
//        }else if(visibility==GONE){
//            setGone();
//        }
//    }

    public void setVisibility2(int visibility) {
        if(visibility==VISIBLE){
            setshow();
        }else if(visibility==GONE){
            setGone();
        }
    }
}
